package com.yonyou.brigade.cron.order.ordercenter;

import com.alibaba.fastjson.JSONObject;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.order_center.model.api.BriHotelOrderInfoAPIDTO;
import com.yonyou.brigade.order_center.service.IBriHotelOrderInfoAPIService;
import com.yonyou.brigade.util.resultutil.HttpSendUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/9/12.
 */
@ElasticJobConfig(cron = "0 0 1 * * ? ", shardingTotalCount = 1)
//@ElasticJobConfig(cron = "0 */1 * * * ?", shardingTotalCount = 1)
@Component
public class SendHotelOrders implements SimpleJob {

    Logger  log= LoggerFactory.getLogger(this.getClass());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    @Qualifier("briHotelOrderInfoAPIServiceImpl")
    private IBriHotelOrderInfoAPIService briHotelOrderInfoAPIService;

    @Value("${brigade.url.noaccess}")
    private String url;

    @Override
    public void execute(ShardingContext shardingContext){
        Map<String,String> parmas = new HashMap<>();
        Date date = new Date();
        String endTs = sdf.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1);
        date = calendar.getTime();
        String startTs = sdf.format(date);
        parmas.put("startTs",startTs);
        parmas.put("endTs",endTs);
        List<BriHotelOrderInfoAPIDTO> briHotelOrderInfoAPIDTOS=briHotelOrderInfoAPIService.getBriHotelOrderInfoAPIDTOS(parmas);
        if(briHotelOrderInfoAPIDTOS!=null&&briHotelOrderInfoAPIDTOS.size()>0)
        {
            try {
                Map<String,Object> sendMap = new HashMap<>();
                sendMap.put("code","0");
                sendMap.put("BriHotelOrderInfoAPIDTO",briHotelOrderInfoAPIDTOS);
                sendMap.put("information","操作成功！");
                String jsonStr = JSONObject.toJSONString(sendMap);
                HttpSendUtils.sendPost(url,jsonStr);
            }
            catch (Exception e)
            {
                log.error(e.getMessage());
            }
        }
    }

}
